package com.education.platform.management.service.impl;

import com.education.platform.common.enums.PriceTypeEnum;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.dao.ArticleDao;
import com.education.platform.dao.entity.ArticleDO;
import com.education.platform.dao.model.ArticleModel;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.push.PushUtil;
import com.education.platform.management.service.ArticleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* describe: 文章
* @author : 
* @date: 2020-07-06 09:23:16.228
**/
@Service
public class ArticleServiceImpl extends BaseServiceImpl<ArticleDao,ArticleDO> implements ArticleService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ArticleServiceImpl.class);

    @Autowired
	private ArticleDao articleDao;
    @Autowired
    private PushUtil pushUtil;
    /**
     * 查询文章列表
     * @param query
     * @return
     */
    @Override
    public List<ArticleModel> getArticleList(ArticleModel query) {
        return articleDao.getArticleListBms(query);
    }
    /**
     * 保存文章
     * @param articleDO
     */
    @Override
    public void save(ArticleDO articleDO) {
        if(articleDO == null){
            throw new BusinessException("数据为空");
        }
        //新增
        if(articleDO.getId()==null){
            articleDO.setDeleted(false);
            articleDO.setCreateTime(new Date());
            articleDO.setCreateBy(ThreadUser.getUserName());
            articleDO.setLikeCount(0);
            articleDO.setCollectCount(0);
            articleDO.setSelectedCommentCount(0);
            articleDO.setViewNumber(0);
            //默认为豆子
            articleDO.setPriceType(PriceTypeEnum.BEAN.getValue());
            if(articleDO.getPrice()==null){
                articleDO.setPrice(0f);
            }
            //销量默认为0
            articleDO.setSalesVolume(0);

            articleDO.setDeleted(false);
            articleDO.setPublish(false);
            articleDao.insert(articleDO);

        }else{
            articleDO.setUpdateBy(ThreadUser.getUserName());
            articleDO.setUpdateTime(new Date());
            articleDao.updateById(articleDO);
        }
    }


}
